<?php
namespace Admin\Controller;
use Org\Util;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 稽核控制器
 * @author zhangwei
 */
class InspectionController extends AdminController {
    
    // 物流奖惩条例列表
    public function regulations(){
        $name = I('name');
        $map['status']  =   1;
        if(!$name){
            $name = '';
        }
        $map['department'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('regulations', $map,'createAt asc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    
    
    // 新增物流奖惩条例
    public function regulationsadd($title="",$content="",$score="",$department=""){
        if(IS_POST){
            $regulation = array('title' => $title, 'content' => $content, 'score' => $score, 'department' => $department, 'createAt' => NOW_TIME,'updateAt'=>NOW_TIME);
            $id = M('regulations')->add($regulation);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('regulations'));
            }
        }else{
            $this->display();
        }
    }
    
    // 奖惩条例编辑
    public function regulationsedit($title="",$content="",$score="") {
        $id = I('id');
        if (IS_POST) {
            $regulation = array('title' => $title, 'content' => $content, 'score' => $score);
            if (!M('regulations')->where(array('id' => $id))->save($regulation)) {
                $this->error("修改失败！");
            } else {
                $this->success('修改成功！', U('regulations'));
            }
        } else {
            $data = M('regulations')->where(array('id' => $id))->find();
            $this->data = $data;
            $this->display();
        }
    }
    
    // 奖惩条例删除
    public function regulationsdel($id = 0) {
        if ($id) {
            $regulations = M("regulations");
            
            $data = array('status'=>-1);
            $result = $regulations-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('regulations'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
    
    
    // 物流部奖惩列表
    public function logistics(){
        $name = I('name');
        $map['status']  =   1;
        $department = '物流';
        $map['department'] = array('like', '%' . (string) $department . '%');
        if($name){
            $map['logisticsname'] = array('like', '%' . (string) $name . '%');
        }
        
        $list = $this->lists('inspection', $map,'createAt asc');
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '数据列表';
        $this->display();
    }
    
    // 新增物流奖惩
    public function logisticsadd($regulationId="",$logisticsname="",$content="",$score=""){
        if(IS_POST){

            $lguserarray = explode(',',$logisticsname);
            $lguserid = $lguserarray[0];
            $lgusername = $lguserarray[1];
            
            $inspection = array('regulationId' => $regulationId, 'content' => $content, 'score' => $score, 'happendTime'=>NOW_TIME,
                'lgusername'=>$lgusername,'lguserid'=>$lguserid,'department' => '物流部', 'createAt' => NOW_TIME,'updateAt'=>NOW_TIME);
            $id = M('inspection')->add($inspection);
            if (!$id) {
                $this->error("添加失败！");
            } else {
                $this->success('添加成功！', U('logistics'));
            }
        }else{
            $map['department']=array('in','物流部,果然100通用条例');
            //$regulations = $this->lists('regulations', $map,'createAt asc');
            $regulations = M('regulations')->where($map)->select();
            $this->assign('regulations',$regulations);
            
            $AeQuery = new Util\AEQuery("api","lg_courier");
            $AeObjectArray = $AeQuery->find("status=1");
            $lguserarr = array();

            for($i=0;$i<count($AeObjectArray);$i++){
                $id = $AeObjectArray[$i]->get("id");
                $name = $AeObjectArray[$i]->get("name");
                $lguserarr[$i]['id'] = $id;
                $lguserarr[$i]['name'] = $name;
            }
            $this->assign('lguserarr',$lguserarr);
            
            $this->display();
        }
    }
    
    // 物流奖惩删除
    public function logisticsdel($id = 0) {
        if ($id) {
            $inspections = M("inspection");
            
            $data = array('status'=>-1);
            $result = $inspections-> where(array('id'=>$id))->setField($data);
            
            if($result){
                $this->success('删除成功！', U('logistics'));
            }else{
                $this->error("删除失败！");
            }
        }
    }
}
